package cn.wenhe9.yygh.user.api;

import cn.wenhe9.yygh.cmn.client.DictFeignClient;
import cn.wenhe9.yygh.common.result.ResultResponse;
import cn.wenhe9.yygh.common.utils.AuthContextHolder;
import cn.wenhe9.yygh.model.user.UserInfo;
import cn.wenhe9.yygh.model.vo.user.LoginVo;
import cn.wenhe9.yygh.model.vo.user.UserAuthVo;
import cn.wenhe9.yygh.user.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author DuJinliang
 * 2022/4/4
 */
@Slf4j
@Api(tags = "用户信息接口")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Resource
    private DictFeignClient dictFeignClient;

    @Resource
    private UserInfoService userInfoService;

    /**
     * 用户手机号登录
     * @param loginVo LoginVo
     * @return ResultResponse
     */
    @ApiOperation("用户手机号登录")
    @PostMapping("/login")
    public ResultResponse login(@RequestBody LoginVo loginVo){
        return userInfoService.loginUser(loginVo);
    }

    /**
     * 用户认证接口
     * @param userAuthVo UserAuthVo
     * @param request HttpServletRequest
     * @return ResultResponse
     */
    @ApiOperation("用户认证接口")
    @PostMapping("/auth/userAuth")
    public ResultResponse userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request){
        //传递两个参数
        return userInfoService.userAuth(AuthContextHolder.getUserId(request), userAuthVo);
    }

    /**
     * 获取用户id信息接口
     * @param request HttpServletRequest
     * @return ResultResponse
     */
    @ApiOperation("获取用户id信息接口")
    @GetMapping("/auth/userInfo")
    public ResultResponse getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        String certificatesTypeString = dictFeignClient.getName("CertificatesType", userInfo.getCertificatesType());
        Map<String, Object> map = new HashMap<>();
        map.put("certificatesTypeString", certificatesTypeString);
        userInfo.setParam(map);
        return ResultResponse.ok(userInfo);
    }
}
